package com.android.project.games;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import com.android.project.framework.FileIO;



public class Settings {

	    public static boolean soundEnabled = true;
	    public final static int[] highscores = new int[] { 100, 80, 50, 30, 10 };
	    public final static String file = ".associate";
	    public static boolean twitter = false;
	    public static boolean facebook = false;
	    public static int score;

	    public static void load(FileIO files) {
	        BufferedReader in = null;
	        try {
	            in = new BufferedReader(new InputStreamReader(files.readFile(file)));
	            soundEnabled = Boolean.parseBoolean(in.readLine());
	            twitter = Boolean.parseBoolean(in.readLine());
	            facebook = Boolean.parseBoolean(in.readLine());
	            score = Integer.parseInt(in.readLine());
	            for(int i = 0; i < 5; i++) {
	                highscores[i] = Integer.parseInt(in.readLine());
	            }
	        } catch (IOException e) {
	            // :( It's ok we have defaults
	        } catch (NumberFormatException e) {
	            // :/ It's ok, defaults save our day
	        } finally {
	            try {
	                if (in != null)
	                    in.close();
	            } catch (IOException e) {
	            }
	        }
	    }

	    public static void save(FileIO files) {
	        BufferedWriter out = null;
	        try {
	            out = new BufferedWriter(new OutputStreamWriter(
	                    files.writeFile(file)));
	            out.write(Boolean.toString(soundEnabled));
	            out.write("\n");
	            out.write(Boolean.toString(twitter));
	            out.write("\n");
	            out.write(Boolean.toString(facebook));
	            out.write("\n");
	            out.write(Integer.toString(score));
	            out.write("\n");
	            for(int i = 0; i < 5; i++) {
	                out.write(Integer.toString(highscores[i]));
	                out.write("\n");
	            }

	        } catch (IOException e) {
	        } finally {
	            try {
	                if (out != null)
	                    out.close();
	            } catch (IOException e) {
	            }
	        }
	    }

	    public static void addScore(int score) {
	        for(int i=0; i < 5; i++) {
	            if(highscores[i] < score) {
	                for(int j= 4; j > i; j--)
	                    highscores[j] = highscores[j-1];
	                highscores[i] = score;
	                break;
	            }
	        }
	    }
	    
	    public static void viewScore(int s){
	    	score = s;
	    	
	    }

}
